#Google App Engine's modules
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template
from google.appengine.ext.webapp.util import login_required
from google.appengine.api import users

#Parsing modules
import gdata.youtube    
import gdata.youtube.service

def GetAndPrintVideoFeed(uri,c_url,c_title):
  yt_service = gdata.youtube.service.YouTubeService()
  feed = yt_service.GetYouTubeVideoFeed(uri)
  for entry in feed.entry:
    try:
      c_url.append(entry.media.content[0].url)
      c_title.append(entry.media.title.text)
    except:
      pass

#helper function to get text that is not within a tag i.e. <title> Title here </title>
def getText(nodelist):
  rc = u""
  for node in nodelist:
      if node.nodeType == node.TEXT_NODE:
          rc = rc + node.data
  return rc

#retrieves title,code and thumbnail of video
def handleContent(feed,titles,playerCode,thumbnails):
  for entry in feed.getElementsByTagName('entry'):
    for title in entry.getElementsByTagName("media:title"):
      titles.append(getText(title.childNodes))
    for name in entry.getElementsByTagName("yt:videoid"):
      playerCode.append(getText(name.childNodes))
    for title in entry.getElementsByTagName("media:thumbnail"):
      if "default" in title.getAttribute('url'):
        if "hqdefault" not in title.getAttribute('url'):
          thumbnails.append(title.getAttribute('url'))

#retrieves title of video
def handleContent2(feed,t):
  for entry in feed.getElementsByTagName('title'):
    return getText(entry.childNodes)